package com.onpositive.datamap.values.impl;

import java.util.Arrays;

import com.onpositive.data.diskstorage.AbstractValueAccessor;
import com.onpositive.datamap.IDataPropertyValue;
import com.onpositive.datamap.core.IDataProperty;

public class ValueSequence extends BasicValue implements IDataPropertyValue,Comparable<ValueSequence> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected ValueSequence(){
		
	}

	
	protected String[] values;

	public ValueSequence(IDataProperty prop, Object comment, String[] value) {
		super(prop, comment);
		this.values = value;
	}

	public Object[] getParts() {
		return values;
	}

	@Override
	public String toString() {
		if (values.length == 0) {
			return "";
		}
		if (values.length == 1) {
			return values[0];
		}
		String string = Arrays.toString(values);
		return string.substring(1,string.length()-1);
	}

	// @Override
	// public String toBasicHTML() {
	//
	// }

	@Override
	public void render(ITokenAcceptor token) {
		if (values.length > 0) {
			if (values.length == 1) {
				if (values[0].length() == 0) {
					return;
				}

			}
			int a=0;
			for (String s : values) {
				LinkParser.parseHyperlink(s, token, this.getProperty()
						.getId());
				if (a<values.length-1){
				token.acceptValueSeparator();
				}
				a++;
			}
			return;
		}
		return;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(values);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ValueSequence other = (ValueSequence) obj;
		if (!Arrays.equals(values, other.values))
			return false;
		return true;
	}
	String plain;
	public String getPlain(){
		if (plain==null){
			PlainTextTokenAcceptor token = new PlainTextTokenAcceptor();
			this.render(token);
			plain=token.bld.toString();
		}
		return plain;
	}

	@Override
	public int compareTo(ValueSequence o) {
		if (o==null){
			return -1;
		}
		return this.getPlain().toLowerCase().compareTo(o.getPlain().toLowerCase());
	}

	@Override
	public IDataPropertyValue clone(AbstractValueAccessor accessor) {
		return new ValueSequence(prop.clone(accessor),getComment(),values);
	}
	
	
}
